<?php

class Core_Model_Road_Collection_Info extends Dcmp_Model_Collection_Strong {

    public function __construct() {
        parent::__construct();
        $this->setItemClass('road/info');
        $this->from('{t}road_info', 'a')
                ->fields('a.*', 'b.province_name', 'b.city_name', 'b.district_name', 'b.townstreet_name', 'b.road_name')
                ->field('b.id', 'road_id')
                ->rightJoin('{t}foundation_road', 'b', 'b.id', 'a.code');
    }

    public function joinAccount() {
        return $this->leftJoin('{t}permission_account', 'c', 'c.id', 'a.creator_id')->field('c.full_name', 'creator_name');
    }

    public function filterByRole(array $roleIds) {
        $this->innerJoin('{t}foundation_road_account', 'd', 'd.road_id', 'b.id')
                ->where()
                ->inQuery('d.account_id')
                ->from('{t}permission_account_role')
                ->contain('role_id', ':roleIds:i')
                ->field('account_id')
                ->addParameter('roleIds', $roleIds)
                ->closest();
        return $this;
    }

    public function filterBy($name, $arg) {
        switch ($name) {
            case 'name':
                return $this->like('b.road_name', ':name:s')->addParameter('name', '%' . $arg . '%');
            case 'status':
                return $this->eq('a.status', ':status:s')->addParameter('status', $arg);
        }
        return $this;
    }

    public function sortBy($name, $dir) {
        switch ($name) {
            case 'id':
                return $dir == 'asc' ? $this->asc('a.id') : $this->desc('a.id');
        }
        return parent::sortBy($name, $dir);
    }

}